package com.qf.boot2202day03.aspect;

import com.qf.boot2202day03.exhandler.ParamNullException;
import com.qf.boot2202day03.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class NullValidateAspect {

    @Pointcut("@annotation(com.qf.boot2202day03.annotation.NullValidation)")
    public void mypc(){};

    @Before("mypc()")
    public void before(JoinPoint joinPoint){

        log.debug("校验参数是否为空");
        Object[] args = joinPoint.getArgs();

        for (Object arg : args) {
            if(arg==null){
                log.debug("参数空了");
                throw new ParamNullException(Constants.PARAM_NULL_CODE,"参数为空");
            }
        }
    }
}
